package com.yyaccp.appinfo.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        response.addHeader( "Access-Control-Allow-Origin", "*" );
        if(request.getSession().getAttribute(Constants.LOGIN_USER) == null) {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String json = JSONObject.toJSONString(AjaxResult.error("请先登录"));
            out.print(json);
            out.flush();
            return false;
        }
           return true;
    }
}
